import { ref } from 'vue'
import { sparePartApplyPosting } from '@/api/store'

/**
 * 配件申请单过账服务
 */
export default function () {
  const sparePartApplyPostingLoading = ref(false)
  const sparePartApplyPostingForm = ref<StorageAPIRequest.SparePartApplyPostingForm>({
    saleNo: '',
    code: '',
    contactName: '',
    contactPhone: '',
    createTime: '',
    createType: 1,
    detailAddress: '',
    factory: '',
    id: '',
    materialApplyVoList: [],
    networkName: '',
    orderRemark: '',
    receivingStoreId: '',
    shippingStoreId: '',
    status: 1,
    type: 1,
    updateTime: '',
    networkId: '',
    receivingStoreName: '',
    shippingStoreName: '',
    province: '',
    city: '',
    county: '',
    area: '',
    town: '',
    creator: '',
    freezeAmount: 0,
    isPosting: 0,
    spareApplyOrderLogList: [],
  })

  const resetSparePartApplyPostingForm = () => {
    sparePartApplyPostingForm.value = {
      saleNo: '',
      code: '',
      contactName: '',
      contactPhone: '',
      createTime: '',
      createType: 1,
      detailAddress: '',
      factory: '',
      id: '',
      materialApplyVoList: [],
      networkName: '',
      orderRemark: '',
      receivingStoreId: '',
      shippingStoreId: '',
      status: 1,
      type: 1,
      updateTime: '',
      networkId: '',
      receivingStoreName: '',
      shippingStoreName: '',
      province: '',
      city: '',
      county: '',
      area: '',
      town: '',
      creator: '',
      freezeAmount: 0,
      isPosting: 0,
      spareApplyOrderLogList: [],
    }
  }

  const saveSparePartApplyPosting = async (messageId: string) => {
    try {
      sparePartApplyPostingLoading.value = true
      const res = await sparePartApplyPosting(sparePartApplyPostingForm.value, messageId)
      sparePartApplyPostingLoading.value = false
      return res.data
    } catch (e) {
      sparePartApplyPostingLoading.value = false
    }
  }

  return {
    sparePartApplyPostingForm,
    sparePartApplyPostingLoading,
    saveSparePartApplyPosting,
    resetSparePartApplyPostingForm,
  }
}
